/**
 * @Project Name:hoofungson-elk-log
 * @Package Name:cn.hoofungson.framework.elk.log.util
 * @Since JDK 1.8
 */

package cn.hoofungson.framework.elk.log.util;

import cn.hoofungson.framework.elk.log.constant.json.JsonConstants;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @Description: MapUtils
 * @Author 胡松 hoofungson@163.com
 * @Date 2019-08-30 15:04
 * @Version V1.0
 */
public class MapUtils {

    /**
     * 格式化为JSON
     *
     * @param map
     * @return
     **/
    public static String formatJSON(Map<String, String> map) {
        if (null == map) {
            return StringUtils.EMPTY;
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(JsonConstants.JSON_START_SYMBOL);
        map.forEach((k, v) -> {
            if (StringBufferUtils.isNotBlank(stringBuffer)
                    && !StringBufferUtils.isEndWith(stringBuffer, JsonConstants.JSON_START_SYMBOL)) {
                stringBuffer.append(",");
            }
            stringBuffer.append("\"");
            stringBuffer.append(k);
            stringBuffer.append("\":\"");
            stringBuffer.append(v);
            stringBuffer.append("\"");
        });
        stringBuffer.append(JsonConstants.JSON_END_SYMBOL);
        return stringBuffer.toString();
    }
}
